/*
 * Copyright (c) 2011 Seoul National University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jung Ho Ahn  ( gajh@snu.ac.kr )
 *
 */

#include <iostream>
#include <iomanip>
#include <cstring>
#include <ctime>
#include <string>
#include <sys/time.h>
#include "Graph.h"
#include "KmerHashTable.h"
#include "InputOptions.h"

using namespace std;

void print_and_update_elapsed_time(
    const string & output_str,
    timeval & prev_time,
    timeval & curr_time,
    timeval & start_time);


int main(int argc, char *argv[])
{
  timeval start_time, prev_time, curr_time;
  gettimeofday(&prev_time, NULL);
  start_time = curr_time = prev_time;

  string  optionfile;
  bool    is_cin_empty = true;

  // parse input arguments
  for (int i = 0; i < argc; i++)
  {
    if (argv[i] == string("-optionfile"))
    {
      i++;
      optionfile = argv[i];
    }
    else if (argv[i] == string("-check_cin"))
    {
      is_cin_empty = false;
    }
    else if (argv[i] == string("-h"))
    {
      cout << "usage: " << argv[0] << " -optionfile <machine description file> -check_cin" << endl;
      return 1;
    }
  }

  InputOptions * options    = new InputOptions(optionfile, is_cin_empty);
  KmerCoverageTable * table = new KmerCoverageTable(options);
  table->processReads();
  print_and_update_elapsed_time("short read -> kmer takes      ", prev_time, curr_time, start_time);

  if (options->get_param_bool("create_histogram"))
  {
    table->displayKmerCountHistogram();
    print_and_update_elapsed_time("histogram buildup takes       ", prev_time, curr_time, start_time);
  }

  if (options->get_param_bool("exit_after_kmer_buildup"))
  {
    exit(1);
  }

  cout << "Populating Kmer Nodes" << endl;
  table->populateKmerNodes();
  print_and_update_elapsed_time("populating kmers takes        ", prev_time, curr_time, start_time);

  cout << "Linking kmers" << endl;
  table->linkKmers();
  print_and_update_elapsed_time("kmer linking takes            ", prev_time, curr_time, start_time);

  table->initializeContigs();
  cout << *table << endl;

  if (options->get_param_bool("display_nodes_after_initializing_contigs"))
  {
    table->displayNodes();
  }

  print_and_update_elapsed_time("contig initialization takes   ", prev_time, curr_time, start_time);

  Graph * graph = new Graph(table);
  graph->findLikelyNeighbors();

  if (options->get_param_bool("display_nodes_after_finding_likely_neighbors"))
  {
    graph->displayNodes();
  }
  print_and_update_elapsed_time("finding likely neighbors takes", prev_time, curr_time, start_time);
  table->runInteractiveMode();

  graph->resolveConflicts();
  if (options->get_param_bool("display_nodes_after_resolving_conflicts_in_graphs"))
  {
    graph->displayNodes();
  }
  print_and_update_elapsed_time("resolving conflicts takes     ", prev_time, curr_time, start_time);

  graph->extractContig();
  print_and_update_elapsed_time("contig extraction takes       ", prev_time, curr_time, start_time);

  table->displayContigSizeHistogram(); 

  return 0;
}


void print_and_update_elapsed_time(
    const string & output_str,
    timeval & prev_time,
    timeval & curr_time,
    timeval & start_time)
{
  gettimeofday(&curr_time, NULL);
  cout << "  curr: "
       << setw(7) << setiosflags(ios::fixed) << setprecision(2)
       << (curr_time.tv_sec - start_time.tv_sec) + 0.000001 * (curr_time.tv_usec - start_time.tv_usec) << "s, "
       << output_str << " "
       << setw(7) << setiosflags(ios::fixed) << setprecision(2)
       << (curr_time.tv_sec - prev_time.tv_sec) + 0.000001 * (curr_time.tv_usec - prev_time.tv_usec) << "s" << endl;
  prev_time = curr_time;
}

